import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      redirect: '/dashboard'
    },
    {
      path: '/dashboard',
      name: 'Dashboard',
      component: () => import('@/views/Dashboard.vue'),
      meta: { title: '仪表盘' }
    },
    {
      path: '/modules',
      name: 'Modules',
      component: () => import('@/views/Modules.vue'),
      meta: { title: '模块列表' }
    },
    {
      path: '/module/:id',
      name: 'ModuleDetail',
      component: () => import('@/views/ModuleDetail.vue'),
      meta: { title: '模块详情' }
    },
    {
      path: '/analysis',
      name: 'Analysis',
      component: () => import('@/views/Analysis.vue'),
      meta: { title: '数据分析' }
    },
    {
      path: '/memory',
      name: 'Memory',
      component: () => import('@/views/Memory.vue'),
      meta: { title: '内存分析' }
    },
    {
      path: '/symbols',
      name: 'Symbols',
      component: () => import('@/views/Symbols.vue'),
      meta: { title: '符号分析' }
    },
    {
      path: '/dependencies',
      name: 'Dependencies',
      component: () => import('@/views/Dependencies.vue'),
      meta: { title: '依赖关系' }
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('@/views/NotFound.vue'),
      meta: { title: '页面未找到' }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - OHOS架构分析系统`
  }
  next()
})

// 全局路由错误处理
router.onError((error) => {
  console.error('Router navigation error:', error)
  // 防止路由错误导致应用崩溃
})

export default router